/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventasautomotrices;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author Chapas
 */
public class VentaAutos extends javax.swing.JFrame implements ActionListener{

    /**
     * Creates new form VentaAutos
     */
    private ControlDatos cdatos = new ControlDatos(); //controla acceso a datos
    private ControlVentas cventas = new ControlVentas(); // controla operaciones de ventas
    private AdminConexion aconect = new AdminConexion();
    Empleado emp = new Empleado();
    
    public VentaAutos( Empleado emp) {
        this.emp = emp;
        
        initComponents();
        llenarcliente();
        llenarautos();
        
        this.lidvendedor.setText(""+emp.getIdUsuario());
        this.cauto.addActionListener(this);
        this.ccliente.addActionListener(this);
        updateVendedor(emp.getIdUsuario());
        
        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.lfecha.setText(sdf.format(dt));
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cauto = new javax.swing.JComboBox();
        lcolor = new javax.swing.JLabel();
        lplaca = new javax.swing.JLabel();
        lmarca = new javax.swing.JLabel();
        lmodelo = new javax.swing.JLabel();
        llinea = new javax.swing.JLabel();
        lidvendedor = new javax.swing.JLabel();
        ccliente = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        ltipo = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lnombrecliente = new javax.swing.JLabel();
        lapellidocliente = new javax.swing.JLabel();
        lfecha = new javax.swing.JLabel();
        tnit = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtInstRegistro = new javax.swing.JTextArea();
        lapellidovendedor = new javax.swing.JLabel();
        lnombrevendedor = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Formulario de Venta");

        jLabel2.setText("Cliente:");

        jLabel3.setText("Vendedor:");

        jLabel6.setText("Id Automovil:");

        jLabel7.setText("Marca:");

        jLabel8.setText("Modelo:");

        jLabel9.setText("Línea:");

        jLabel10.setText("Color:");

        jLabel11.setText("Placa:");

        lcolor.setText("----");

        lplaca.setText("----");

        lmarca.setText("----");

        lmodelo.setText("----");

        llinea.setText("----");

        lidvendedor.setText("---");

        jLabel12.setText("Tipo:");

        ltipo.setText("----");

        jLabel13.setText("Fecha:");

        jLabel14.setText("NIT:");

        jLabel4.setText("Nombre:");

        jLabel5.setText("Apellido:");

        lfecha.setText(" ");

        tnit.setText("CF");
        tnit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tnitActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jScrollPane1.setEnabled(false);

        txtInstRegistro.setEditable(false);
        txtInstRegistro.setColumns(20);
        txtInstRegistro.setRows(5);
        txtInstRegistro.setText("          Añadir Venta\n\n1) Seleccione el ID de cliente\n\n2) Llene el NIT\n\n3) Seleccione el ID del automovil\n\n4) Presione el botón Guardar");
        jScrollPane1.setViewportView(txtInstRegistro);

        jLabel15.setText("Apellido:");

        jLabel16.setText("Nombre:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel2)
                        .addGap(48, 48, 48)
                        .addComponent(ccliente, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(lnombrecliente, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lapellidocliente, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(184, 184, 184)
                        .addComponent(btnGuardar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(jLabel14))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addComponent(jLabel6)
                                .addGap(26, 26, 26)
                                .addComponent(cauto, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(80, 80, 80)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel10)
                                                .addGap(61, 61, 61)
                                                .addComponent(lcolor)
                                                .addGap(132, 132, 132))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addGap(61, 61, 61)
                                                .addComponent(llinea)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel11)
                                                .addGap(61, 61, 61)
                                                .addComponent(lplaca))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel12)
                                                .addGap(66, 66, 66)
                                                .addComponent(ltipo))))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addGap(57, 57, 57)
                                        .addComponent(lmarca)
                                        .addGap(132, 132, 132)
                                        .addComponent(jLabel8)
                                        .addGap(52, 52, 52)
                                        .addComponent(lmodelo))))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(25, 25, 25)
                                        .addComponent(jLabel3))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(30, 30, 30)
                                        .addComponent(jLabel13)))
                                .addGap(35, 35, 35)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lidvendedor)
                                    .addComponent(tnit, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(59, 59, 59)
                                .addComponent(jLabel16)
                                .addGap(18, 18, 18)
                                .addComponent(lnombrevendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lapellidovendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)))
                .addGap(48, 48, 48))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel2))
                            .addComponent(lnombrecliente, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ccliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5)
                                .addComponent(lapellidocliente, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addComponent(lidvendedor)))
                                .addGap(16, 16, 16)
                                .addComponent(jLabel13))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lnombrevendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel16)
                                        .addComponent(jLabel15)
                                        .addComponent(lapellidovendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(4, 4, 4)
                                .addComponent(lfecha)))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(tnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel6))
                            .addComponent(cauto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(lmarca)
                            .addComponent(jLabel8)
                            .addComponent(lmodelo)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(ltipo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(lplaca)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(llinea))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(lcolor))))
                .addGap(45, 45, 45)
                .addComponent(btnGuardar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tnitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tnitActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tnitActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        boolean insertado = saveSale();        // TODO add your handling code here:
        if( insertado ){
            JOptionPane.showMessageDialog(this, "La Venta Se ha guardado.", "Exito", JOptionPane.PLAIN_MESSAGE);
            this.dispose();
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cauto;
    private javax.swing.JComboBox ccliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lapellidocliente;
    private javax.swing.JLabel lapellidovendedor;
    private javax.swing.JLabel lcolor;
    private javax.swing.JLabel lfecha;
    private javax.swing.JLabel lidvendedor;
    private javax.swing.JLabel llinea;
    private javax.swing.JLabel lmarca;
    private javax.swing.JLabel lmodelo;
    private javax.swing.JLabel lnombrecliente;
    private javax.swing.JLabel lnombrevendedor;
    private javax.swing.JLabel lplaca;
    private javax.swing.JLabel ltipo;
    private javax.swing.JTextField tnit;
    private javax.swing.JTextArea txtInstRegistro;
    // End of variables declaration//GEN-END:variables

    private void llenarcliente() {
       try {
            
           aconect.abrirConexionBDD();
            ResultSet clientes = aconect.consulta("SELECT * FROM AutoVentas.Cliente");
            while (clientes.next()){
            this.ccliente.addItem(clientes.getObject("idCliente"));
            }
            aconect.cerrarConexionBDD();
        } catch (Exception e) {
           System.out.println("Fallo llenar clientes: " + e);
        }
        
        
    }

    private void llenarautos() {
        try {
            
            aconect.abrirConexionBDD();
            ResultSet clientes = aconect.consulta("SELECT * FROM AutoVentas.Automovil");
            while (clientes.next()){
            this.cauto.addItem(clientes.getObject("idautomovil"));
            }
            aconect.cerrarConexionBDD();
        } catch (Exception e) {
           System.out.println("Fallo llenar clientes: " + e);
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
               
        if (ae.getSource().equals(this.cauto)){
            int id = Integer.parseInt(cauto.getSelectedItem().toString());
            updateAuto(id);
        }else if (ae.getSource().equals(this.ccliente)){
            int id = Integer.parseInt(ccliente.getSelectedItem().toString());
            updateCliente(id);
        }
    }
    
    private void updateAuto( int id) {
    
        try {
            
            aconect.abrirConexionBDD();
            ResultSet auto = aconect.consulta("SELECT * FROM Automovil WHERE idAutomovil = " + id);
            while (auto.next()){
                this.lmarca.setText(auto.getObject("marca").toString());
                this.lmodelo.setText(auto.getObject("modelo").toString());
                this.lcolor.setText(auto.getObject("color").toString());
                this.llinea.setText(auto.getObject("linea").toString());
                this.ltipo.setText(auto.getObject("tipo").toString());
                this.lplaca.setText(auto.getObject("placa").toString());
                
            }
            aconect.cerrarConexionBDD();
        } catch (Exception e) {
           System.out.println("Fallo llenar clientes: " + e);
        }
    }

    private void updateCliente(int id) {
        try {
            
            aconect.abrirConexionBDD();
            ResultSet cliente = aconect.consulta("SELECT * FROM Cliente WHERE idCliente = " + id);
            while (cliente.next()){
                this.lnombrecliente.setText(cliente.getObject("nombre").toString());
                this.lapellidocliente.setText(cliente.getObject("apellido").toString());
            
            }
            aconect.cerrarConexionBDD();
        } catch (Exception e) {
           System.out.println("Fallo llenar clientes: " + e);
        }
    }
    
    private void updateVendedor(int id) {
        try {
            
            aconect.abrirConexionBDD();
            ResultSet vendedor = aconect.consulta("SELECT * FROM Empleado WHERE idVendedor IN (SELECT Empleado_idVendedor FROM AutoVentas.Usuarios WHERE idUsuarios =" + id +")" );
            while (vendedor.next()){
                this.lnombrevendedor.setText(vendedor.getObject("nombre").toString());
                this.lapellidovendedor.setText(vendedor.getObject("apellido").toString());
                
            }
            aconect.cerrarConexionBDD();
        } catch (Exception e) {
           System.out.println("Fallo llenar clientes: " + e);
        }
    }
    
    public boolean saveSale(){
        
        boolean insertado = false;
        String fecha = this.lfecha.getText();
        String nit = this.tnit.getText();
        int cliente = Integer.parseInt(this.ccliente.getSelectedItem().toString());
        int auto = Integer.parseInt(this.cauto.getSelectedItem().toString());
        int vendedor = this.emp.getIdUsuario();
        //System.out.println("Fecha: "+ fecha + "  Nit:" + nit + "  Cliente: " +cliente + "  Vendedor: " + vendedor + "  Auto:" + auto);;
        insertado = cventas.InsertarVenta(fecha, nit, cliente, vendedor,auto);
        if (!insertado){
            JOptionPane.showMessageDialog(this, "no se pudo hacer la venta");
        }

        return insertado;
    }
    
}
